package com.chencq.core.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 *  字符串处理类
 *  
 * @author chencq
 *
 */
public class StringUtils {

	/**
	 * 非空验证
	 * 
	 * @param value
	 * @return 空 true 非空false
	 */
	public static final boolean isNull(String value) {
		boolean result = false;
		if (value == null || value.trim().length() == 0 || value.equals("null")) {
			result = true;
		}
		return result;
	}

	/**
	 * 将以&分隔 name=value格式的字符串，转成Map
	 * 
	 * @param queryString
	 * @return Map
	 */
	public static Map<String, String> toQueryParams(String queryString) {
		Map<String, String> ht = new HashMap<String, String>();
		if (queryString != null) {
			String[] params = queryString.split("&");
			for (int i = 0; i < params.length; i++) {
				String[] pairs = params[i].split("=");
				if (pairs.length > 1) {
					ht.put(pairs[0], pairs[1]);
				}
			}
		}
		return ht;
	}

	/**
	 * 判断是否是GB2312 
	 * @param str
	 * @return
	 */
	public static boolean isGB2312(String str) {
		char[] chars = str.toCharArray();
		boolean f = false;
		for (int i = 0; i < chars.length; i++) {
			byte[] bytes = ("" + chars[i]).getBytes();
			if (bytes.length == 2) {
				int[] ints = new int[2];
				ints[0] = bytes[0] & 0xff;
				ints[1] = bytes[1] & 0xff;
				if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40
						&& ints[1] <= 0xFE) {
					f = true;
					break;
				}
			}
		}
		return f;
	}

	/**
	 * 首字符大写
	 * @param str
	 * @return
	 */
	public static String firstCharToUpperCase(String str) {
		String upperCase = str.substring(0, 1).toUpperCase();
		str = upperCase + str.substring(1);
		return str;
	}

	/**
	 * 首字符小写
	 * @param str
	 * @return
	 */
	public static String firstCharToLowerCase(String str) {
		String upperCase = str.substring(0, 1).toLowerCase();
		str = upperCase + str.substring(1);
		return str;
	}

	// -----------UUID------------

	private final static char[] digits = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
			'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
			'X', 'Y', 'Z' };

	/**
	 * 获取一个伪 UUID.
	 * <p>
	 * ID长度为12时，当系统中已有十亿笔数据，那么要再插入大约3000笔，才会发生一次重复。
	 * ID长度为16时，即使系统中已有百亿笔数据，那么还要再插入大约5亿笔，才会发生一次重复。
	 * 
	 * @param size
	 *            UUID的长度, 推荐 >=12
	 * @return 返回指定长度的UUID
	 */
	public static String newUUID(int size) {
		Random random = new Random();
		char[] cs = new char[size];
		for (int i = 0; i < cs.length; i++) {
			cs[i] = digits[random.nextInt(digits.length)];
		}
		return new String(cs);
	}

	/**
	 * 获得短的伪 UUID.
	 * <p>
	 * 长度 16位
	 * @return UUID值
	 */
	public static String newShortUUID() {
		return newUUID(16);
	}

	/**
	 * 获得32位的UUID值.
	 * 
	 * @return 32位的UUID值.
	 */
	public static String randomUUID() {
		// TIP: 还有压缩到19位的算法
		String id = UUID.randomUUID().toString();
		return id;
	}
	
	
	/**
	 * 二行数据制转换成十六进制字符串.
	 * 
	 * @param b byte[] 字节流
	 * @return String 十六进制字符串
	 * @since BASE 0.1
	 */
	public static String byteToHexString(byte[] b) { //

		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			}
			else {
				hs = hs + stmp;
			}

			if (n < b.length - 1) hs = hs + "";
		}
		// return hs.toUpperCase();
		return hs;
	}
	
	/**
	 * 将字符串编码成UNICODE编码格式字符串.
	 * 
	 * @param s 原字符串.
	 * @return 编码后的字符串.
	 */
	public static String string2Unicode(String s) {
		if (s == null || s.length() == 0) return "";
		char[] charA = s.toCharArray();
		StringBuilder t = new StringBuilder("");
		String tt = "";
		for (int i = 0; i < charA.length; i++) {
			tt = Integer.toHexString((int) charA[i]);
			if (tt.length() == 2)
				tt = "%" + tt;
			else
				tt = "%u" + tt;
			t.append(tt);
		}
		return t.toString();
	}

	/**
	 * 把字符串改为新字符集编码
	 * 
	 * @param s
	 * @param charSetName
	 */
	public static String encodeString(String s, String charSetName) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			}
			else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes(charSetName);
				}
				catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0) k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}
	
	/**
	 * 获取类的短名称
	 * 
	 * @param className 类名称
	 * @return 类名短名称
	 */
	public static String getShortClassName(String className) {
		int pos = className.lastIndexOf(".");
		if (pos > 0) className = className.substring(pos + 1, className.length());
		return className;
	}

	
	
}
